package com.bitcoin.scheduler.task;

import com.bitcoin.scheduler.service.BlockccService;
import com.bitcoin.scheduler.service.MarketBinanceApiService;
import com.bitcoin.scheduler.service.MarketGateIoApiService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

@Service
@Slf4j
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class SyncBlockccKlineTask implements Job {
    @Autowired
    BlockccService blockccService;
    @Autowired
    MarketBinanceApiService binanceApiService;
    @Autowired
    MarketGateIoApiService gateIoApiService;
    @Autowired
    private ObjectMapper jsonMapper;
    private final static Map<String, Integer> symbols = Maps.newLinkedHashMap();

    static {
        symbols.put("BTC", 1);
        symbols.put("ETC", 17);
        symbols.put("QTUM", 18);
        symbols.put("ETH", 2);
        symbols.put("XRP", 3);
        symbols.put("ONT", 31);
        symbols.put("EOS", 5);
        symbols.put("NEO", 11);
        symbols.put("IOTA", 10);
        symbols.put("AE", 27);
        symbols.put("ZEC", 28);
        symbols.put("BTM", 32);
        symbols.put("NAS", 66);
        symbols.put("BCD", 39);
        symbols.put("BNB", 22);
        symbols.put("TRX", 9);
        symbols.put("XLM", 8);
        symbols.put("ADA", 6);
        symbols.put("LTC", 7);
        symbols.put("BAT", 54);
        symbols.put("BCX", 19);
        symbols.put("BOT", 374);
        symbols.put("BTG", 26);
        symbols.put("BTO", 124);
        symbols.put("BTS", 34);
        symbols.put("HT", 138);
        symbols.put("CDT", 235);
        symbols.put("DCR", 49);
        symbols.put("DGD", 51);
        symbols.put("ELF", 55);
        symbols.put("FIL", 24);
        symbols.put("GNT", 60);
        symbols.put("GTC", 146);
        symbols.put("GXS", 90);
        symbols.put("IHT", 201);
        symbols.put("LINK", 103);
        symbols.put("LRC", 53);
        symbols.put("MANA", 104);
        symbols.put("MCO", 106);
        symbols.put("MKR", 44);
        symbols.put("NANO", 29);
        symbols.put("PAY", 109);
        symbols.put("POWR", 100);
        symbols.put("RUFF", 141);
        symbols.put("SNT", 50);
        symbols.put("WAVES", 43);
        symbols.put("XVG", 25);
        symbols.put("ZIL", 41);
        symbols.put("ZRX", 46);
        symbols.put("ZSC", 372);
    }

    @Override
    @Transactional
    public void execute(JobExecutionContext context) throws JobExecutionException {
        String[] markets = {"binance", "gate-io"};

        List<Future> futures = Lists.newArrayList();
        for (String symbol : symbols.keySet()) {
//            for (int limit = 120; limit >= 58; limit -= 1) {
            for (String market : markets) {
                try {
                    log.info("开始同步{} -----{}", market, symbol);
                    if (!blockccService.getSymbolPairsDao().existsByMarketNameAndSymbolAndCurrency(market, symbol, "USDT")) {
                        log.info("不存在USDT  {} -----{}", market, symbol);
                        continue;
                    }
                    futures.add(blockccService.sync(market, symbol, 1));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
//            }
        }
        for (Future future : futures) {
            try {
                future.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
